package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.TanHuaService;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tanhua")
@Slf4j
public class TanHuaController {

    @Autowired
    private TanHuaService tanHuaService;

    /**
     * 查询个人主页的个人信息
     *
     * @param userId  要查看的那个用户id
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity<TodayBest> queryUserInfo(@PathVariable("id") Long userId){
        try {
            TodayBest todayBest = tanHuaService.queryUserInfo(userId);
            if (ObjectUtil.isNotEmpty(todayBest)){
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            log.error("查询个人主页出错! userId = "  + userId , e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    private ResponseEntity<String> queryQuestion(@RequestParam("userId") Long userId){
        try {
            String question = tanHuaService.queryQuestion(userId);
            return ResponseEntity.ok(question);
        } catch (Exception e) {
            log.error("查询陌生人问题出错! userId = "  + userId , e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 回复陌生人问题
     *
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replyQuestion(@RequestBody Map<String, Object> map){
        try {
            Long userId = Long.valueOf(map.get("userId").toString()); //陌生人的id
            String reply = map.get("reply").toString();
            Boolean result = tanHuaService.replyQuestion(userId,reply);
            if (result){
                return ResponseEntity.ok(null);
            }
        } catch (NumberFormatException e) {
            log.error("回答陌生人问题出错! userId = "  + map.get("userId") , e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜附近
     *
     * @param gender  性别
     * @param distance  距离
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender" , required = false) String gender,
                                                          @RequestParam(value = "distance" , defaultValue = "2000") String distance){
        try {
            List<NearUserVo> nearUserVoList = tanHuaService.queryNearUser(gender,distance);
            if (ObjectUtil.isNotEmpty(nearUserVoList)){
                return ResponseEntity.ok(nearUserVoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardsList(){
        try {
            List<TodayBest> list = tanHuaService.queryCardsList();
            if(ObjectUtil.isNotEmpty(list)){
                return ResponseEntity.ok(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> likeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.tanHuaService.likeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> notLikeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.tanHuaService.notLikeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
